package com.zhihao.baobao.recketdemo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * 火箭主体类
 * 
 * @author wissea
 * 
 */
public class Rocket extends View {

	private Bitmap bitmap;
	private Bitmap rocket;
	// 文字栏的三种状态
	private Bitmap[] stand = {
			BitmapFactory.decodeResource(getResources(),
					R.drawable.desktop_bg_tips_1),
			BitmapFactory.decodeResource(getResources(),
					R.drawable.desktop_bg_tips_2),
			BitmapFactory.decodeResource(getResources(),
					R.drawable.desktop_bg_tips_3) };
	private Matrix matrix = new Matrix();
	private Paint paint = new Paint();

	// 是否触摸
	private boolean isTouch = false;
	private Point point = new Point();

	private LocationChangeListener changeListener = null;
	// 记录屏幕的大小
	private int screenW;
	private int screenH;

	// 灰机和提示是否重合了
	private boolean isFly = false;

	public Rocket(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public Rocket(Context context, AttributeSet attrs) {
		super(context, attrs);
		bitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.floating_desktop_bg_danger);
		rocket = BitmapFactory.decodeResource(getResources(),
				R.drawable.desktop_rocket_launch_1);
		screenW = ((Activity) context).getWindowManager().getDefaultDisplay()
				.getWidth();
		screenH = ((Activity) context).getWindowManager().getDefaultDisplay()
				.getHeight();
	}

	public Rocket(Context context) {
		super(context);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		// 判断Touch
		if (!isTouch) {
			matrix.reset();
			if (isFly) {
				if (changeListener != null)
					changeListener.onchange();
			} else {
				if (point.x < screenW / 2) {
					matrix.postTranslate(0, point.y);
				} else {
					matrix.postTranslate(screenW - bitmap.getWidth(), point.y);
				}
				canvas.drawBitmap(bitmap, matrix, paint);
			}
		} else {
			matrix.reset();
			int c = point.x % 2;
			matrix.postTranslate(screenW / 2 - stand[c].getWidth() / 2, screenH
					- stand[c].getHeight() - 100);
			// 判断重合
			if (Math.abs(screenW / 2 - (point.x - rocket.getWidth() / 2)) < stand[c]
					.getWidth() / 2
					&& Math.abs(screenH - stand[c].getHeight() - 100
							- (point.y - rocket.getHeight())) < stand[c]
								.getHeight()) {
				canvas.drawBitmap(stand[2], matrix, paint);
				isFly = true;
			} else {
				isFly = false;
				canvas.drawBitmap(stand[c], matrix, paint);
			}
			matrix.reset();
			matrix.postTranslate(point.x - rocket.getWidth() / 2, point.y
					- rocket.getHeight());
			canvas.drawBitmap(rocket, matrix, paint);
		}

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			// 过滤无效点击
			if ((event.getX() < screenW - bitmap.getWidth() && event.getX() > bitmap
					.getWidth())
					|| Math.abs(point.y - event.getY()) > bitmap.getHeight()) {
				return false;
			}
			isTouch = true;
			break;
		case MotionEvent.ACTION_MOVE:
			// 更新坐标
			point.x = (int) event.getX();
			point.y = (int) event.getY();
			break;
		case MotionEvent.ACTION_CANCEL:
			isTouch = false;
			break;
		case MotionEvent.ACTION_UP:
			isTouch = false;
			break;
		}
		invalidate();
		return true;
	}

	// 重置状态
	public void reset() {
		isFly = false;
		point.y = screenH / 2;
		invalidate();
	}

	// 监听重合
	public void setOnChangeListener(LocationChangeListener changeListener) {
		this.changeListener = changeListener;
	}

}
